package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2018/4/13.
 */

public class RecommendTopDownloadV2View extends LinearLayout {

    private TextView topDownloadNumTx;
    private ImageView topDownloadIv;
    private LinearLayout topDownloadRootLl;

    private int totalDownloadNum;

    public RecommendTopDownloadV2View(Context context) {
        super(context);
        initView(context);
    }

    public RecommendTopDownloadV2View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.part_top_download_v2_view, null);
        topDownloadRootLl = rootView.findViewById(R.id.topDownloadRootLl);
        topDownloadIv = rootView.findViewById(R.id.topDownloadIv);
        topDownloadNumTx = rootView.findViewById(R.id.topDownloadNumTx);

        setBackgroundAlpha(0);
        int w = (int) getContext().getResources().getDimension(R.dimen.wdp68);
        int h = (int) getContext().getResources().getDimension(R.dimen.wdp70);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(w, h);
        topDownloadRootLl.setLayoutParams(params);

        this.removeAllViews();
        this.addView(rootView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.getInstance().goDownloadCenterActivity(getContext());
            }
        });
    }

    //0:全透明，255：不透明
    public void setBackgroundAlpha(@IntRange(from=0,to=255)int alpha) {
        Drawable collBg = topDownloadRootLl.getBackground();
        collBg.setAlpha(alpha);
        topDownloadRootLl.setBackgroundDrawable(collBg);
        if (alpha==0) {
            topDownloadIv.setImageResource(R.mipmap.ic_top_download_v2);
        } else {
            topDownloadIv.setImageResource(R.mipmap.ic_top_download_v2_yy);
        }
        invalidate();
    }

    public int getTotalDownloadNum() {
        return totalDownloadNum;
    }

    public void setTotalDownloadNum() {
        try {
            DownloadTaskManager downloadTaskManager = DownloadTaskManager.getInstance();
            List<DownLoadTaskTable> dataList = downloadTaskManager.findNotDownFinishNumber();
            if (dataList!=null && dataList.size()>0) {
                setTotalDownloadNum(dataList.size());
            } else {
                setTotalDownloadNum(0);
            }
        } catch (Exception e) {
            LogUtils.e("RecommendTopDownloadV2View>>>>e:"+e.getMessage());
        }
    }

    public void setTotalDownloadNum(int totalDownloadNum) {
        this.totalDownloadNum = totalDownloadNum;
        topDownloadNumTx.setText(""+totalDownloadNum);
        invalidate();
    }
}
